package com.yuelao.yuelao_backend.controller;

import com.yuelao.yuelao_backend.common.ApiResponse;
import com.yuelao.yuelao_backend.common.BizException;
import com.yuelao.yuelao_backend.common.ErrorCode;
import com.yuelao.yuelao_backend.dto.GroupDTO;
import com.yuelao.yuelao_backend.service.GroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/groups")
@Slf4j
public class GroupController {

    @Resource
    private GroupService groupService;

    /**
     * 获取群聊列表
     * GET /groups
     * @return 群聊列表
     */
    @GetMapping
    public ApiResponse<List<GroupDTO>> getGroupList() {
        List<GroupDTO> groupList = groupService.getGroupList();
        return ApiResponse.ok(groupList);
    }

    /**
     * 获取群聊详细信息
     * GET /groups/{groupId}
     * @param groupId 群聊ID
     * @return 群聊信息
     */
    @GetMapping("/{groupId}")
    public ApiResponse<GroupDTO> getGroupInfo(@PathVariable Long groupId) {
        if (groupId == null || groupId <= 0) {
            throw new BizException(ErrorCode.PARAMS_ERROR, "群聊ID无效");
        }
        GroupDTO groupDTO = groupService.getGroupInfo(groupId);
        return ApiResponse.ok(groupDTO);
    }

    /**
     * 获取群聊公告
     * GET /groups/{groupId}/announcement
     * @param groupId 群聊ID
     * @return 群聊公告内容
     */
    @GetMapping("/{groupId}/announcement")
    public ApiResponse<String> getGroupAnnouncement(@PathVariable Long groupId) {
        if (groupId == null || groupId <= 0) {
            throw new BizException(ErrorCode.PARAMS_ERROR, "群聊ID无效");
        }
        String announcement = groupService.getGroupAnnouncement(groupId);
        return ApiResponse.ok(announcement);
    }
}